<html>
<head><meta charset="utf-8"><title>global allocator · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html">global allocator</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202885651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885651">(Jul 04 2020 at 18:24)</a>:</h4>
<p>Hello folks... I have a question about the Global Allocator. I'm implementing my own, and the docs suggest this example: <a href="https://doc.rust-lang.org/core/alloc/trait.GlobalAlloc.html">https://doc.rust-lang.org/core/alloc/trait.GlobalAlloc.html</a></p>



<a name="202885653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885653">(Jul 04 2020 at 18:24)</a>:</h4>
<p>However, I cannot use <code>std</code></p>



<a name="202885659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885659">(Jul 04 2020 at 18:24)</a>:</h4>
<p>Can one call <code>alloc</code> from within crate <code>core</code>?</p>



<a name="202885674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885674">(Jul 04 2020 at 18:25)</a>:</h4>
<p>If I switch that example to use <code>core</code> then the compiler says <code>alloc</code> does not exist</p>



<a name="202885682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885682">(Jul 04 2020 at 18:25)</a>:</h4>
<p>std, core and alloc are all separate crates but std re-exports them both.</p>



<a name="202885694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885694">(Jul 04 2020 at 18:26)</a>:</h4>
<p>I can't quite remember how to enable alloc however</p>



<a name="202885748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885748">(Jul 04 2020 at 18:27)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="cp">#![no_std]</span><span class="w"></span>
<span class="k">extern</span><span class="w"> </span><span class="k">crate</span><span class="w"> </span><span class="n">alloc</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">alloc</span>::<span class="n">alloc</span>::<span class="p">{</span><span class="n">alloc</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">alloc_fn</span><span class="p">,</span><span class="w"> </span><span class="n">GlobalAlloc</span><span class="p">,</span><span class="w"> </span><span class="n">Layout</span><span class="p">};</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">ptr</span>::<span class="n">null_mut</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">MyAllocator</span><span class="p">;</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="n">GlobalAlloc</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">MyAllocator</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">alloc</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_layout</span>: <span class="nc">Layout</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">null_mut</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">dealloc</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_ptr</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">_layout</span>: <span class="nc">Layout</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[global_allocator]</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">A</span>: <span class="nc">MyAllocator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MyAllocator</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">alloc_fn</span><span class="p">(</span><span class="n">Layout</span>::<span class="n">new</span>::<span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">()).</span><span class="n">is_null</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="202885757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885757">(Jul 04 2020 at 18:27)</a>:</h4>
<p>Ah yes ^^</p>



<a name="202885763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885763">(Jul 04 2020 at 18:27)</a>:</h4>
<p><code>extern crate alloc</code> magically imports the <code>alloc</code> crate</p>



<a name="202885767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885767">(Jul 04 2020 at 18:27)</a>:</h4>
<p>Its just a regular <code>extern crate alloc</code> and It'll be implied.</p>



<a name="202885809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885809">(Jul 04 2020 at 18:28)</a>:</h4>
<p>Yeah I was looking at the features I was using instead like <code>allocator_api</code> <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="202885887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885887">(Jul 04 2020 at 18:30)</a>:</h4>
<p>Ah, many thanks.</p>



<a name="202885890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885890">(Jul 04 2020 at 18:30)</a>:</h4>
<p>I was wondering what happened to it...</p>



<a name="202885989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202885989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202885989">(Jul 04 2020 at 18:33)</a>:</h4>
<p><code>alloc as alloc_fn</code> this is needed... because <code>alloc</code> is ambiguous (either the crate or this symbol)?</p>



<a name="202886000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202886000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202886000">(Jul 04 2020 at 18:33)</a>:</h4>
<p>yeah, somewhat unfortunate <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="202886040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202886040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202886040">(Jul 04 2020 at 18:34)</a>:</h4>
<p>Can we rename the crate instead?</p>



<a name="202886043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202886043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202886043">(Jul 04 2020 at 18:34)</a>:</h4>
<p>you can also use <code>alloc::alloc::alloc</code></p>



<a name="202886044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202886044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Merritt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202886044">(Jul 04 2020 at 18:34)</a>:</h4>
<p>Ah I'll just do that</p>



<a name="202886047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202886047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202886047">(Jul 04 2020 at 18:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="318622">Alex Merritt</span> <a href="#narrow/stream/122651-general/topic/global.20allocator/near/202886040">said</a>:</p>
<blockquote>
<p>Can we rename the crate instead?</p>
</blockquote>
<p>yeah</p>



<a name="202991497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/global%20allocator/near/202991497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/global.20allocator.html#202991497">(Jul 06 2020 at 13:45)</a>:</h4>
<p>It seems like borderline "a bug" that the docs for alloc refer to things being imported through std.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>